import {reqAddRoom, reqDeleteRoom, reqGetRoomLIstData, reqModifyRoomConfig} from "@/api";

const state = {
    roomListData:[]
}
const actions = {
    // 新增房间
    async addRoom({commit}, roomInfo){
        let result = await reqAddRoom(roomInfo);
        if (result.code == 200){
            this._vm.$message({
                showClose:true,
                message:'房间创建成功。',
                type:'success'
            })
        }else{
            this._vm.$message({
                showClose:true,
                message:'房间创建失败，请联系管理员。',
                type:'error'
            })
        }
    },

    // 获取房间列表数据
    async getRoomListData({commit}){
        let result = await reqGetRoomLIstData();
        if (result.code == 200){
            commit('GETROOMLISTDATA', result.data);
        }else{
            this._vm.$message({
                showClose:true,
                message:'获取房间列表失败',
                type:"error"
            })
        }
    },

    // 修改房间配置
    async modifyRoomConfig({commit}, config){
        let result = await reqModifyRoomConfig(config);
        if (result.code == 200){
            this._vm.$message({
                showClose:true,
                message:'房间配置更新成功！',
                type:"success"
            })
        }else{
            this._vm.$message({
                showClose:true,
                message:'房间配置更新失败！',
                type:"error"
            })
        }
    },

    // 删除房间
    async deleteRoom({commit}, config){
        let result = await reqDeleteRoom(config);
        if (result.code == 200){
            this._vm.$message({
                showClose:true,
                message:'房间删除成功！',
                type:"success"
            })
        }else{
            this._vm.$message({
                showClose:true,
                message:'房间删除失败，请联系管理员！',
                type:"error"
            })
        }
    }
}
const mutations = {
    GETROOMLISTDATA(state, data){
        console.log('roomData---roomListData数据获取成功。');
        state.roomListData = data;
    }
}
const getters = {
    // 获取待开房的房间列表（state：0）
    getCanOpenRoomList() {
        return Object.forEachToArr(state.roomListData, (item, index, key) => {
            if (item.state === '0'){
                return Object.assign({}, {
                    roomId:item.roomId,
                    roomNum:item.roomNum,
                    roomType:item.roomType,
                    roomPrice:item.price,
                    state:item.state,
                })
            }
        })
    },
    // 获取待退房的房间列表（state：1）
    getCanCloseRoomList() {
        return Object.forEachToArr(state.roomListData, (item, index, key) => {
            if (item.state === '1'){
                return Object.assign({}, {
                    roomId:item.roomId,
                    roomNum:item.roomNum,
                    roomType:item.roomType,
                    roomPrice:item.price,
                    state:item.state,
                    orderId:item.orderId
                })
            }
        })
    },
}

export default {
    state,
    actions,
    mutations,
    getters
}